using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.Core.DAL;

namespace Portal.GUI.EditoralOffice.MainOffce.NewsEditModules.RelatedNews
{
    public partial class RelatedNewsEdit : Portal.BO.Editoral.NewsEditModules.NewsModule
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindData();
            }
        }

        private void bindData()
        {
            LoadRuntimeSettings();

            string newsIdStr = Convert.ToString(ReadRuntimeConfig(true, "NewsID")).Trim();
            if (newsIdStr != "")
            {
                Hashtable table = getNews(newsIdStr);
                string[] newsIds = newsIdStr.Split(',');
                foreach (string newsId in newsIds)
                {
                    cboNews.Items.Add(new ListItem(Convert.ToString(table[newsId]), newsId));
                }
            }

            txtTitle.Text = Convert.ToString(ReadRuntimeConfig(true, "Title"));
            txtTitleColor.Text = Convert.ToString(ReadRuntimeConfig(true, "TitleColor"));
            txtTitleBackgroundColor.Text = Convert.ToString(ReadRuntimeConfig(true, "TitleBackgroundColor"));
            txtTitleHeight.Text = Convert.ToString(ReadRuntimeConfig(true, "TitleHeight"));
            txtNewsHeight.Text = Convert.ToString(ReadRuntimeConfig(true, "NewsHeight"));
            txtWidth.Text = Convert.ToString(ReadRuntimeConfig(true, "Width"));
            txtBackgroundColor.Text = Convert.ToString(ReadRuntimeConfig(true, "BackgroundColor"));
            txtBorderWidth.Text = Convert.ToString(ReadRuntimeConfig(true, "BorderWidth"));
            txtBorderColor.Text = Convert.ToString(ReadRuntimeConfig(true, "BorderColor"));
            txtPaddingLeft.Text = Convert.ToString(ReadRuntimeConfig(true, "PaddingLeft"));
            txtPaddingRight.Text = Convert.ToString(ReadRuntimeConfig(true, "PaddingRight"));
            txtPaddingTop.Text = Convert.ToString(ReadRuntimeConfig(true, "PaddingTop"));
            txtPaddingBottom.Text = Convert.ToString(ReadRuntimeConfig(true, "PaddingBottom"));
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
			save();
			((Portal.GUI.EditoralOffice.MainOffce.editnews.editModule)this.Page).loadPresentationModule(ModuleType, ModuleRef);
        }

        public override void SaveAllProperties()
        {
            save();
        }

        private void save()
        {
            PortalDefinition.Module module = new PortalDefinition.Module();
            module.reference = this.ModuleRef;
            module.type = this.ModuleType;
            module.LoadRuntimeProperties();

            string newsIdStr = hdRelatNews.Value.Trim();
            //foreach (ListItem item in cboNews.Items)
            //{
            //    newsIdStr += item.Value + ",";
            //}
            if (newsIdStr.EndsWith(","))
                newsIdStr = newsIdStr.Remove(newsIdStr.LastIndexOf(","));
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "NewsID", newsIdStr);

            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "Title", txtTitle.Text);
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "TitleColor", txtTitleColor.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "TitleBackgroundColor", txtTitleBackgroundColor.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "TitleHeight", txtTitleHeight.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "NewsHeight", txtNewsHeight.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "Width", txtWidth.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "BackgroundColor", txtBackgroundColor.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "BorderColor", txtBorderColor.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "BorderWidth", txtBorderWidth.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "PaddingLeft", txtPaddingLeft.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "PaddingRight", txtPaddingRight.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "PaddingTop", txtPaddingTop.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "PaddingBottom", txtPaddingBottom.Text.Trim());

            module.SaveRuntimeSettings();
        }

        private Hashtable getNews(string newsIds)
        {
            Hashtable table = new Hashtable();
            using (MainDB db = new MainDB())
            {
                string sql = "Select News_ID, News_Title from News where News_ID in (" + newsIds + ")";
                DataTable dt = db.SelectQuery(sql);
                foreach (DataRow row in dt.Rows)
                {
                    table.Add(Convert.ToString(row["News_ID"]), Convert.ToString(row["News_Title"]));
                }
                db.Close();
            }
            return table;
        }

    }
}